/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __LIB2D_GUI_DESKTOP_H__
#define __LIB2D_GUI_DESKTOP_H__

#include <input/Listener.h>
#include <input/Event.h>
#include <input/Manager.h>
#include "Object.h"

namespace liba
{
namespace lib2d
{
namespace gui
{

class Desktop :
	public Object,
	private input::Listener
{
	Rect main_screen;
	Vector2 get_mouse_pos( const std::string & hardware_name )const
	{
		double x = input::Manager::get_axis_position( hardware_name, "Axis_X" );
		double y = input::Manager::get_axis_position( hardware_name, "Axis_Y" );
		x = (x + 1)/2 * main_screen.width;
		y = (y + 1)/2 * main_screen.height;
		return Vector2( x, y );
	}
public:
	Desktop( const Rect & window_rect, const Rect & main_screen )
	:	Object( 0, window_rect ),
		main_screen( main_screen )
	{}
	void change_windowrect( const Rect & window_rect )
	{
		this->window_rect = window_rect;
		layout();
	}
	void on_mode_change( const Rect & main_screen )
	{
		this->main_screen = main_screen;
	}
	virtual void on_input_event( const input::Event & event )
	{
		if( event.get_key_name() == "Axis_X" || event.get_key_name() == "Axis_Y" )
		{
			Vector2 point = get_mouse_pos( event.get_hardware_name() );
			if( window_rect.is_intersects( point ) )
				on_mouse_move( point - window_rect.get_coord() );
		}
		else
		if( event.get_key_name() == "Left Button" )
		{
			Vector2 point = get_mouse_pos( event.get_hardware_name() );
			if( window_rect.is_intersects( point ) )
			{
				if( event.is_pressed() )
					on_button_down( point - window_rect.get_coord(), Atom() );
				else
					on_button_up( point - window_rect.get_coord(), Atom() );
			}
		}
		else
		if( event.is_text() )
		{
			on_text( event.get_text() );
		}
		else
		if( event.is_pressed() )
		{
			on_key_down( event.get_key_name() );
		}
	}
};

} //namespace gui
} //namespace lib2d
} //namespace liba

using namespace liba;

#endif //__LIB2D_GUI_DESKTOP_H__

